//
//  Login_PWD_VC.swift
//  旅航ios
//
//  Created by tulip on 2021/5/17.
//

import UIKit
import SwiftyJSON
import HandyJSON

class Login_PWD_VC: UIViewController {
    @IBOutlet weak var phone: UITextField!
    
    @IBOutlet weak var postBtn: UIButton!
    @IBOutlet weak var pwd: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        postBtn=gradientColor(colo1: "ff8e4a", colo2: "ff6635", view: postBtn,width: SCREEN_WIDTH-30) as? UIButton
        postBtn.layer.cornerRadius=6
        postBtn.layer.masksToBounds=true
        
        
    }

    @IBAction func loginByMessage(_ sender: Any) {
        let vc=Login_mes_VC.init()
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    
    @IBAction func regist(_ sender: Any) {
        let vc=RegistVC.init()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    @IBAction func post(_ sender: Any) {
        let dic=["account":phone.text ?? "","password":pwd.text ?? ""]
//        let dic = ["account":"yanhuan","password":"qwe123"]
        
        
        Network.request(.login(parameters: dic)) { (JSON) in
//            YProgressHUD.showToast(JSON["msg"].string)
            if JSON["code"].int == 1 {
                
                let token =  JSON["data"]["userinfo"]["token"].string
                
                if(token == nil){
                    YProgressHUD.showToast("链接错误")
                    return
                }
                UserDefaults.standard.setValue(token, forKey: "token")
                UserDefaults.standard.setValue(true, forKey: "isLogin")
                
                
               
                guard let model = JSONDeserializer<User>.deserializeFrom(json: "\(JSON["data"]["userinfo"])") else {
                    return
                }
                
                print("***\(model.token)")
                
                UserManager.shared.currentUser = model
                UserManager.shared.saveUserinfo()
       
                TOKEN=token ?? ""
                let vc=MTTabBarController.init()
                UIApplication.shared.keyWindow?.rootViewController=vc
               
            }
        } failure: { (err) in
            
        }
        
        
        
        
    }
    override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden=true
    }
}
